using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace SAInterfaceLibrary
{
    /// <summary>
    /// Simple logger to write errors out to the Application event log.
    /// </summary>
    public class EventLogger
    {
        static private EventLogEntryType _minimum_level = EventLogEntryType.Information;

        static public EventLogEntryType MinimumErrorLevel
        {
            get { return _minimum_level; }
            set { _minimum_level = value; }
        }


        public static void LogException(string message, Exception info, EventLogEntryType level, int error_id)
        {
            StringBuilder bld = new StringBuilder();
            bld.Append(message);
            bld.Append(" - ");
            bld.Append(ExceptionToString(info));
            LogMessage(bld.ToString(), level, error_id);
        }

        /// <summary>
        /// The instance of the log we are going to use. No need to do this unless we really need to!
        /// </summary>
        static EventLog _log = null;

        /// <summary>
        /// Just log a simple string message to the event log.
        /// </summary>
        /// <param name="p"></param>
        public static void LogMessage(string message, EventLogEntryType level, int error_id)
        {
            ///
            /// Should we even log this?
            /// 

            if (_minimum_level == EventLogEntryType.Error)
            {
                if (level != EventLogEntryType.Error)
                {
                    return;
                }
            } else if (_minimum_level == EventLogEntryType.Warning) {
                if (level != EventLogEntryType.Warning || level != EventLogEntryType.Error)
                {
                    return;
                }
            }

            ///
            /// Log the message
            /// 

            Init();
            _log.WriteEntry(message, level, error_id);
        }

        /// <summary>
        /// Hook up the event log if it hasn't been done already.
        /// </summary>
        private static void Init()
        {
            if (_log == null)
            {
                _log = new EventLog("Application");
                _log.Source = "ROOT Builder";
            }
        }

        /// <summary>
        /// If 0, 1 or 2 is passed in, sets the value to the apropriate level. Otherwise does
        /// nothing.
        /// </summary>
        /// <param name="min_level"></param>
        public static void LoadMinLevelFromInt (int min_level)
        {
            switch (min_level)
            {
                case 0:
                    _minimum_level = EventLogEntryType.Information;
                    break;

                case 1:
                    _minimum_level = EventLogEntryType.Warning;
                    break;

                case 2:
                    _minimum_level = EventLogEntryType.Error;
                    break;
            }
        }

        /// <summary>
        /// Format an exception as a string. off to the side because of the potential
        /// recursive nature of the thing.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        static private string ExceptionToString(Exception e)
        {
            StringBuilder bld = new StringBuilder();
            bld.AppendLine(e.Message);
            bld.AppendLine(" at ");
            bld.AppendLine(e.StackTrace);
            if (e.InnerException != null)
            {
                bld.Append(" -- Inner exception: ");
                bld.Append(ExceptionToString(e.InnerException));
            }
            return bld.ToString();
        }

    }
}
